<?
//User 0.0.3
class user{
    private $vars=array();
    function __construct($login='', $password=''){
        global $kernel;
        $this->login=$login;
        $this->password=$password;
        $this->access_level=0;
        $prefix=$kernel->site->user_autorisation_prefix;
        switch($kernel->site->user_autorisation_type){
            case 'cookies':
                if(!isset($_COOKIE[$prefix.'_un_key'])) setcookie($prefix.'_un_key', data::uniq(7), 0, '/');
                $this->un_key=$_COOKIE[$prefix.'_un_key'];
                if(isset($_COOKIE['login'])&&isset($_COOKIE['password'])){
                    $this->login=$_COOKIE[$prefix.'_login'];
                    $this->password=$_COOKIE[$prefix.'_password'];
                    $this->un_key=$_COOKIE[$prefix.'_un_key'];
                }
                break;
            case 'session': default:
                if(!isset($_SESSION[$prefix.'_un_key'])) $_SESSION[$prefix.'_un_key']=data::uniq(7);
                $this->un_key=$_SESSION[$prefix.'_un_key'];
                if(isset($_SESSION[$prefix.'_login'])&&isset($_SESSION[$prefix.'_password'])){
                    $this->login=$_SESSION[$prefix.'_login'];
                    $this->password=$_SESSION[$prefix.'_password'];
                }
                break;
        }
        if($this->login&&$this->password&&$this->un_key){
            $this->is_sigedin();
        }
    }
    //Signin
    function is_sigedin($login='', $password=''){
        global $sql;
        if($login) $this->login=$login;
        if($password) $this->password=$password;
        $this->login=data::word($this->login);
        $this->password=data::word($this->password, 'en', 0, '\$~@!#%^\&\*\(\)-=\+\[\]\{\}');
        $r=$sql->query_each("SELECT access_level, id FROM `users` WHERE login='".$this->login."' AND MD5(CONCAT(password,'".$this->un_key."'))='".$this->password."' AND is_active=1", true);
        $this->access_level=$r['access_level'];
        $this->id=$r['id'];
        return $this->access_level;
    }
    function signin($login='', $password=''){
        global $sql, $kernel;
        if($login) $this->login=$login;
        if($password) $this->password=$password;
        if($this->is_sigedin()){
            $prefix=$kernel->site->user_autorisation_prefix;
            switch($kernel->site->user_autorisation_type){
                case 'cookies':
                    setcookie($prefix.'_login', $this->login, 0, '/');
                    setcookie($prefix.'_password', $this->password, 0, '/');
                    return true;
                case 'session': default:
                    $_SESSION[$prefix.'_login']=$this->login;
                    $_SESSION[$prefix.'_password']=$this->password;
                    return true;
            }
        }
        return false;
    }
    //Signout
    function signout(){
        global $kernel;
        $prefix=$kernel->site->user_autorisation_prefix;
        unset($_SESSION[$prefix.'_login']);
        unset($_SESSION[$prefix.'_password']);
        $this->login='';
        $this->password='';
        $this->access_level=0;
    }
    //User adding
    function add($vals){
        global $sql;
        return $sql->insert('users', $vals);
    }
    //User locking
    function lock(){
        global $sql;
        return $sql->update('users', array('is_active', 0), array('login'=>$this->login, 'password'=>$this->password), 1);
    }
    //User deleting
    function delete(){
        global $sql;
        return $sql->delete('users', array('login'=>$this->login, 'password'=>$this->password), 1);
    }
    //User editing
    function edit($vals){
        global $sql;
        return $sql->update('users', $vals, array('login'=>$this->login, 'password'=>$this->password));
    }
    public function __get($key){
        return isset($this->vars[$key])?$this->vars[$key]:'';
    }
    public function __set($key, $val){
        $this->vars[$key]=$val;
    }
}
?>